package com.gxa.springboot229.controller;

import com.gxa.springboot229.excption.BusinessException;
import com.gxa.springboot229.pojo.Admin;
import com.gxa.springboot229.pojo.User;
import com.gxa.springboot229.service.impl.AdminServiceImpl;
import com.gxa.springboot229.util.JWTUtil;
import com.gxa.springboot229.util.JsonResult;
import com.gxa.springboot229.util.Md5Utils;
import org.assertj.core.internal.bytebuddy.implementation.bytecode.Throw;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/adm")
public class AdminController {
    @Resource
    AdminServiceImpl adminService;
    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 登录方法
     * @param phone
     * @param Pwd
     * @return
     */
    @PostMapping("/adminLogin")
    public Map adminLogin(@RequestParam("phone")String phone,@RequestParam("Pwd") String Pwd){
        JsonResult jsonResult=new JsonResult();
        JWTUtil jwtUtil = new JWTUtil();
        Map<String,Object> objectMap =new HashMap<>();


        if("".equals(phone) || "".equals(Pwd)){
            throw new BusinessException("电话或密码不能为空");
        }


 //对进入的密码 进行md5 加密处理 后 调用 adminService 登录接口 进行 密码判断
        String md5Password = Md5Utils.md5Password(Pwd,phone);
        //调用对应的service执行登录方法 查询是否有一条数据
        Integer login = adminService.login(phone,md5Password);
        if(login == 0){
            throw new BusinessException("登录失败，手机号或密码错误");
        }
        String token = null;
        List <Admin> findamdin = adminService.findamdin(1);
        for(Admin s:findamdin){
            String Name = s.getName();
            String Iphone = s.getPhone();
            int id = s.getId();
             token = jwtUtil.createJWT(Iphone, Name, 30 * 1000 * 60 * 6 * 24, objectMap);
            jsonResult.put("UserName",Name);
            jsonResult.put("Phone",Iphone);
            jsonResult.put("token",token);
        }


        return jsonResult.getValues();
    }

    /**
     * 忘记密码方法(修改方法)
     * @param phone
     * @param Pwd
     * @param captcha
     * @return
     */
    @PostMapping("/admUpPwd")
    public Map admUpdate(@RequestParam("phone")String phone,@RequestParam("Pwd") String Pwd,@RequestParam("captcha")String captcha){
        if("".equals(phone) || "".equals(Pwd) || "".equals(captcha)){
            throw new BusinessException("数据不能为空");
        }

        JsonResult jsonResult = new JsonResult();
        //验证码是否合法
        ValueOperations<String,Object> operations = redisTemplate.opsForValue();
        Object captchaIn = operations.get("ama:"+phone);
        if(captcha .equals(String.valueOf(captchaIn))){
            Integer reg = adminService.findByPhone(phone);
            if(reg == 1){
                Admin admin = new Admin();
                admin.setId(1);
                //对进入的密码 进行md5 加密处理 后 调用 adminService 登录接口 进行 密码判断
                String md5Password = Md5Utils.md5Password(Pwd,phone);
                admin.setPwd(md5Password);
                int update = adminService.update(admin);
                if(update == 1) {
                    jsonResult.setCode("1");
                    jsonResult.setMsg("密码修改成功！");
                }else {
                    jsonResult.setCode("0");
                    jsonResult.setCode("密码修改失败！");
                }
            }
        }else {
            jsonResult.setCode("0");
            jsonResult.setMsg("验证失败，请重新发送验证码");
        }
        return jsonResult.getValues();
    }



    /**
     * 修改方法 可以修改手机号 和密码
     * @param map
     * @return
     */
    @PostMapping("/admUpdate")
    public Map admUpdate(@RequestBody Map map){
        JsonResult jsonResult=new JsonResult();
        Admin admin = new Admin();
        if("".equals(map.get("id"))||"".equals(map.get("phone")) || "".equals(map.get("Pwd"))){
            throw new BusinessException("数据不能为空");
        }

        int id = (int)map.get("id");
        admin.setId(id);
        //判断手机号是否为空
        if(!"".equals(map.get("phone"))&&map.get("phone")!=null){
            String phone = (String) map.get("phone");
            //判断 短信验证码 是否为空
            if(map.get("captcha") == null || "".equals(map.get("captcha"))){
                throw  new BusinessException("验证码不能为空");
            }
            //从传入的map 拿取验证码 从Reids 进行验证码的比对 就可以修改手机号码
            String captcha =(String) map.get("captcha");
            ValueOperations<String,Object> operations = redisTemplate.opsForValue();
            Object captchaIn = operations.get("ama:"+phone);
            if(captcha .equals(String.valueOf(captchaIn))) {
                admin.setPhone(phone);
            }else {
                throw new BusinessException("验证码错误!");
            }

        }else
        if(!"".equals(map.get("Pwd")) && map.get("Pwd") != null) {
               // 验证密码不是空 后 取出手机号
            String Pwd = (String) map.get("Pwd");
            List <Admin> findamdin = adminService.findamdin(1);
            String md5Password = null;
            for(Admin s:findamdin){
                // 通过MD5 工具 用原生密码 和手机号 对密码进行加密处理
                String phone = s.getPhone();
                md5Password = Md5Utils.md5Password(Pwd,phone);
            }
            admin.setPwd(md5Password);
        }else {
            throw new BusinessException("传输信息不能为空");
        }

        // 对数据进行修改操作
        int update = adminService.update(admin);
        if(update == 1){
            if(!"".equals(map.get("phone"))&&map.get("phone")!=null){
                List <Admin> findamdin = adminService.findamdin(id);
                jsonResult.setData(findamdin);
            }else {
                jsonResult.setCode("1");
                jsonResult.setMsg("密码修改成功");
            }

        }else{
            throw new BusinessException("修改失败");
        }
        return jsonResult.getValues();
    }


//
//    /**
//     * 查询方法
//     * @param phone
//     * @return
//     */
//    @PostMapping("/admUpdate")
//    public Map admSeclet(@RequestParam("phone")String phone){
//        return null;
//    }
}
